package com.inno.thread.inno.thread;

/**
 * 创建线程方式一：实现Runnable接口
 *
 * @author pretty_boy
 * @date 2018/9/17 16:18
 */
public class RunnableImpl implements Runnable {

    public Thread t;

    private String threadName;

    //无参构造
    public RunnableImpl() {

    }

    //有参构造
    public RunnableImpl(String threadName) {
        this.threadName = threadName;
    }

    @Override
    public void run() {
        try {
            System.out.println("Running..." + threadName);
            for (int i = 1; i <= 5; i++) {
                System.out.println("Thread" + threadName + "," + i);
            }
        } catch (Exception e) {
            System.out.println(threadName + "线程中断。。。");
            e.printStackTrace();
        }
        System.out.println(threadName + "线程结束。。。");
    }


    /**
     * 安全性校验：当线程为null时，会先创建一个线程
     *
     * @Param: []
     * @return: void
     * @Author: pretty_boy
     * @Date: 2018/9/17
     */
    public void start() {

        System.out.println("Starting----" + threadName);
        if (t == null) {
            t = new Thread(this, threadName);
        }
        t.start();
    }
}
